package com.xyw.code.platform.webapps.rest;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xyw.code.common.core.entity.vo.Result;
import com.xyw.code.platform.common.entity.dto.UserDTO;
import com.xyw.code.platform.common.entity.dto.UserInfoDTO;
import com.xyw.code.platform.common.entity.enums.PlatformConstant;
import com.xyw.code.platform.common.entity.po.SysMenu;
import com.xyw.code.platform.common.entity.po.SysUser;
import com.xyw.code.platform.webapps.service.ISysUserService;
import com.xyw.code.platform.webapps.util.EmailUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xyw
 * @since 2020-02-07
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private EmailUtil emailUtil;

    @GetMapping(value = "/query/{username}")
    @ApiOperation(value = "查询用户", notes = "查询用户")
    public Result<List<SysUser>> queryByUsername(@PathVariable(value = "username") String username){
        List<SysUser> sysUsers = userService.lambdaQuery().eq(SysUser::getUsername,username).list();
        return Result.success(sysUsers);
    }

    /**
     * 获取用户列表集合
     *
     * @param userDTO
     * @return
     */
    @GetMapping("/query")
    @ApiOperation(value = "获取用户列表集合", notes = "获取用户列表集合")
    public Result<IPage<SysUser>> query(UserDTO userDTO) {
        return Result.success(userService.getUsersWithRolePage(userDTO));
    }

    /**
     * 保存用户包括角色和部门
     *
     * @param userDto
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "保存用户包括角色和部门", notes = "保存用户包括角色和部门")
    public Result<Boolean> add(@RequestBody UserDTO userDto) {
        return Result.success(userService.insertUser(userDto));
    }


    /**
     * 更新用户包括角色和部门
     *
     * @param userDto
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新用户包括角色和部门", notes = "更新用户包括角色和部门")
    public Result<Boolean> update(@RequestBody UserDTO userDto) {
        return Result.success(userService.updateUser(userDto));
    }


    /**
     * 删除用户包括角色和部门
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除用户包括角色和部门", notes = "删除用户包括角色和部门")
    public Result<Boolean> delete(@PathVariable("id") String id) {
        return Result.success(userService.removeUser(id));
    }



    /**
     * 重置密码
     *
     * @param id
     * @return
     */
    @PutMapping("/restPass/{id}")
    @ApiOperation(value = "重置密码", notes = "重置密码")
    public Result<Boolean> restPass(@PathVariable("id") String id) {
        return Result.success(userService.restPass(id));
    }


    /**
     * 上传用户头像
     * @param file
     * @return
     */
    @PutMapping("/uploadAvatar")
    @ApiOperation(value = "上传用户头像", notes = "上传用户头像")
    public Result<Boolean> uploadAvatar(@RequestParam("file") MultipartFile file) {
        return Result.success(userService.uploadAvatar(file));
    }

    /**
     * 更新用户包括角色和部门
     *
     * @param userDto
     * @return
     */
    @PutMapping("/updateUserInfo")
    @ApiOperation(value = "更新用户包括角色和部门", notes = "更新用户包括角色和部门")
    public Result<Boolean> updateUserInfo(@RequestBody UserInfoDTO userDto) {
        return Result.success(userService.updateUserInfo(userDto));
    }

    /**
     * 重置密码
     ** @return
     */
    @PutMapping("/updatePass")
    @ApiOperation(value = "重置密码", notes = "重置密码")
    public Result<Boolean> updatePass(@RequestParam String oldPass, String newPass) {
        return Result.success(userService.updatePass(oldPass,newPass));
    }

    /**
     * 发送email code
     ** @return
     */
    @PutMapping("/sendMailCode/{email}")
    @ApiOperation(value = "发送email code", notes = "发送email code")
    public Result<Boolean> sendMailCode(@PathVariable String email, HttpServletRequest request) {
        emailUtil.sendSimpleMail(email,request);
        return Result.success(true);
    }



    /**
     * 更改邮箱
     ** @return
     */
    @PutMapping("/updateEmail")
    @ApiOperation(value = "更改邮箱", notes = "更改邮箱")
    public Result<Boolean> updateEmail(@RequestParam String email,@RequestParam String code,HttpServletRequest request) {
        String sessionCode = (String) request.getSession().getAttribute(PlatformConstant.RESET_MAIL );
        return Result.success(userService.updateEmail(email,code,sessionCode));
    }
}

